package com.example.tourism.Controller;

import com.example.tourism.bean.locationSearchReturn;
import com.example.tourism.utils.API.locationSearch_API;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class locationSearchController {
    @Value("${tourism.app.ak}")
    private static String AK ;
//     @PostMapping("/locationSearch")
//     public List<locationSearchReturn> locationSearch(@RequestParam("keyword") String keyword, @RequestParam("region") String region, @RequestParam("location") String location3) throws Exception {
//         List<locationSearchReturn> locationSearchReturnList = new ArrayList<>();
//         locationSearch_API api = new locationSearch_API(AK,keyword,region,location3);
//         String str = api.getResponse();
//         try {
//             ObjectMapper objectMapper = new ObjectMapper();
//             JsonNode rootNode = objectMapper.readTree(str);
//             int status = rootNode.get("status").asInt();
//             if (status == 0) {
//                 JsonNode results = rootNode.get("result");
//                 if (results.isArray()) {
//                     for (JsonNode result : results) {
//                         String name = result.get("name").asText();
//                         JsonNode location = result.get("location");
//                         String address = result.get("address").asText();
//                         locationSearchReturn locationSearchReturn = new locationSearchReturn();
//                         locationSearchReturn.setName(name);
//                         locationSearchReturn.setLocation(location.get("lat").asText()+","+location.get("lng").asText());
//                         locationSearchReturn.setAddress(address);
//                         locationSearchReturnList.add(locationSearchReturn);
//                         // 在这里可以将数据存储到一个数组或列表中
//                     }
//                 }
// //                System.out.println("ok");
//             }
//             else {
// //                System.out.println(status);
//                 return null;
//             }
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//         return locationSearchReturnList;
//     }
}
